#include "udp_server.hpp"

using namespace ns_server;
using namespace std;

static void usage(string proc){
    cout << "Usage:\n\t" << proc << " port\n" << endl;
}

static bool isPass(const string& command){
    bool pass = true;
    auto pos = command.find("rm");
    if(pos != std::string::npos) pass=false;
    pos = command.find("mv");
    if(pos != std::string::npos) pass=false;
    pos = command.find("while");
    if(pos != std::string::npos) pass=false;
    pos = command.find("kill");
    if(pos != std::string::npos) pass=false;
    return pass;
}

string excuteCommand(string command){
    if(!isPass(command))
        return "you are a bad man";
    
    FILE* fp = popen(command.c_str(), "r");
    if(fp == nullptr)
        return "None";
    char line[1024];
    string result;
    while(fgets(line, sizeof(line), fp) != NULL)
        result += line;
    
    pclose(fp);
    return result;
}

string transactionString(string request){
    string result;
    char c;
    for(auto& r : request){
        if(islower(r)){
            c = toupper(r);
            result.push_back(c);
        }
        else
            result.push_back(r);
    }
    return result;
}

int main(int argc, char* argv[]){
    if(argc != 2){
        usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port = atoi(argv[1]);

    //unique_ptr<UdpServer> usvr(new UdpServer(port, transactionString));
    //unique_ptr<UdpServer> usvr(new UdpServer(port, excuteCommand));

    unique_ptr<UdpServer> usvr(new UdpServer(port));

    usvr->Start();
    
    return 0;
}